home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / DelayedSoundArea.java < prev    next >
Text File  |  1998-09-15  |  4KB  |  112 lines

  1. /*
  2.  * @(#)DelayedSoundArea.java    1.9 98/03/18
  3.  *
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.awt.Graphics;
  32. import java.applet.AudioClip;
  33. import java.net.URL;
  34. import java.net.MalformedURLException;
  35. import java.util.StringTokenizer;
  36.  
  37. /**
  38.  * This ImageArea Class will play a sound each time the user enters the 
  39.  * area. It is different from SoundArea in that it accepts a delay (in 
  40.  * tenths of a second) before it plays the sound. If the mouse leaves
  41.  * the area before the time delay, the sound is not played.
  42.  *
  43.  * This allows you to have one piece of audio when the button is "hit"
  44.  * via SoundArea and another if the user stays on the button.
  45.  *
  46.  * @author     Chuck McManis
  47.  * @version     1.9, 03/18/98
  48.  */
  49. class DelayedSoundArea extends ImageMapArea {
  50.     /** The URL of the sound to be played. */
  51.     URL     sound;
  52.     AudioClip    soundData;
  53.     boolean     hasPlayed; 
  54.     int            delay;
  55.     int        countDown;
  56.  
  57.     /**
  58.      * The argument is the URL of the sound to be played.
  59.      * This method also sets this type of area to be non-terminal.
  60.      */
  61.     public void handleArg(String arg) {
  62.     Thread soundLoader;
  63.     StringTokenizer st = new StringTokenizer(arg, ", ");
  64.     
  65.     delay = Integer.parseInt(st.nextToken());
  66.     try {
  67.         sound = new URL(parent.getDocumentBase(), st.nextToken());
  68.     } catch (MalformedURLException e) {
  69.         sound = null;
  70.     }
  71.     }
  72.  
  73.     public void getMedia() {
  74.     if (sound != null) {
  75.         soundData = parent.getAudioClip(sound);
  76.     }
  77.     if (soundData == null) {
  78.         System.out.println("DelayedSoundArea: Unable to load data "+sound);
  79.     }
  80.     }
  81.  
  82.     /**
  83.      * The highlight method plays the sound in addition to the usual
  84.      * graphical highlight feedback.
  85.      */
  86.     public void enter() {
  87.     hasPlayed = false;
  88.     countDown = delay;
  89.     parent.startAnimation();
  90.     }
  91.  
  92.     /**
  93.      * This method is called every animation cycle if there are any
  94.      * active animating areas.
  95.      * @return true if this area requires further animation notifications
  96.      */
  97.     public boolean animate() {
  98.     if (entered && ! hasPlayed) {
  99.         if (countDown > 0) {
  100.         countDown--;
  101.         return true;
  102.         }
  103.         hasPlayed = true;
  104.         if (soundData != null) {
  105.             soundData.play();
  106.         }
  107.     }
  108.     return false;
  109.     }
  110. }
  111.  
  112.